package com.kang.controller;

import com.kang.entity.SysUser;
import com.kang.service.LoginService;
import com.kang.util.WebResponse;
import com.kang.vo.CaptchaLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "用户端登录接口")
@RequestMapping("/user/login")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation("账号密码登录")
    @PostMapping("informationLogin")
    public WebResponse informationLogin(@RequestBody SysUser sysUser) {
        try {
            return WebResponse.resSuccess("账号密码登录成功",loginService.informationLogin(sysUser));
        } catch (Exception ex) {
            log.error("账号密码登录异常",ex);
            return WebResponse.resFail("账号密码登录异常",null);
        }
    }



    @ApiOperation("手机号码验证码登录")
    @PostMapping("phoneLogin")
    public WebResponse phoneLogin(@RequestBody CaptchaLoginVo captchaLoginVo) {
        try {
            return WebResponse.resSuccess("手机号码登录成功",loginService.phoneLogin(captchaLoginVo));
        } catch (Exception ex) {
            log.error("手机号码登录异常",ex);
            return WebResponse.resFail("手机号码登录异常",null);
        }
    }


    @ApiOperation("手机发送验证码")
    @PostMapping("sendSms")
    public WebResponse sendSms(@RequestBody CaptchaLoginVo captchaLoginVo) {
        try {
            return WebResponse.resSuccess("手机发送验证码成功",loginService.sendSms(captchaLoginVo));
        } catch (Exception ex) {
            log.error("手机发送验证码异常",ex);
            return WebResponse.resFail("手机发送验证码异常",null);
        }
    }

    @ApiOperation("手机号码找回密码")
    @PostMapping("findPass")
    public WebResponse findPass(@RequestBody CaptchaLoginVo captchaLoginVo) {
        try {
            return WebResponse.resSuccess("手机号码找回密码成功",loginService.findPass(captchaLoginVo));
        } catch (Exception ex) {
            log.error("手机号码找回密码异常",ex);
            return WebResponse.resFail("手机号码找回密码异常",null);
        }
    }

    @ApiOperation("退出")
    @PostMapping("layout")
    public WebResponse lauout(@RequestBody String token) {
        try {
            return WebResponse.resSuccess("退出成功",loginService.layout(token));
        } catch (Exception ex) {
            log.error("退出失败",ex);
            return WebResponse.resFail("退出失败",null);
        }
    }
}
